package com.seckill.act.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seckill.act.pojo.Activity;
import com.seckill.act.service.ActivityService;
import com.seckill.act.vo.ActivityVo;
import com.seckill.framework.mybatisplus.WrapperParse;
import com.seckill.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 控制器</p>
 *
 * @author 作者
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /***
     * Activity上线/下线
     */
    @PutMapping(value = "/status/{id}/{isup}")
    public Result findPage(@PathVariable(value = "id") String id, @PathVariable(value = "isup") int isup) {
        //上线下线
        activityService.isUp(id, isup);
        return Result.ok();
    }

    /***
     * Activity条件搜索实现
     * @param activity
     * @return
     */
    @PostMapping(value = "/search")
    public Result<ActivityVo> findPage(@RequestBody(required = false) Activity activity) {
        //调用ActivityService实现分页条件查询Activity
        //删除：1未删除，2已删除
        activity.setIsDel(1);
        ActivityVo activityVo = activityService.findPage(activity);
        return Result.ok(activityVo);
    }

    /***
     * Activity分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<Page> findPage(@PathVariable int page, @PathVariable int size) {
        //调用ActivityService实现分页查询Activity
        Page<Activity> pageInfo = activityService.findPage(page, size);
        return Result.ok(pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param activity
     * @return
     */
    //@PostMapping(value = "/search")
    //public Result<List<Activity>> findList(@RequestBody(required = false) Activity activity) {
    //    //调用ActivityService实现条件查询Activity
    //    List<Activity> list = activityService.findList(activity);
    //    return Result.ok(list);
    //}

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        //调用ActivityService实现根据主键删除
        activityService.delete(id);
        return Result.ok();
    }

    /***
     * 修改Activity数据
     * @param activity
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Activity activity, @PathVariable String id) throws ParseException {
        //设置主键值
        activity.setId(id);
        //调用ActivityService实现修改Activity
        activityService.update(activity);
        return Result.ok();
    }

    /***
     * 新增Activity数据
     * @param activity
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Activity activity) throws ParseException {
        //调用ActivityService实现添加Activity
        activityService.add(activity);
        return Result.ok();
    }

    /***
     * 根据ID查询Activity数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Activity> findById(@PathVariable String id) {
        //调用ActivityService实现根据主键查询Activity
        Activity activity = activityService.findById(id);
        return Result.ok(activity);
    }

    /***
     * 查询Activity全部数据
     * @return
     */
    @GetMapping
    public Result<List<Activity>> findAll() {
        //调用ActivityService实现查询所有Activity
        List<Activity> list = activityService.findAll();
        return Result.ok(list);
    }


    /***
     * 时间接口
     */
    @GetMapping(value = "/times")
    public Result<List<Activity>> times() {
        List<Activity> times = activityService.times();
        return Result.ok(times);
    }


    /**
     * 根据时间查询
     */
    @PostMapping(value = "/bgtime")
    public Result<Activity> findByBgtime(@RequestBody Date bgtime) {
        QueryWrapper<Activity> wrapper = WrapperParse.simpleColumn("begintime", bgtime);
        Activity currentActivity = activityService.getOne(wrapper);
        return Result.ok(currentActivity);
    }

}

